package cn.ac.ict.fpevaluation.evaluation;

import cn.ac.ict.fpevaluation.bean.APBean;
import cn.ac.ict.fpevaluation.bean.AtomTrajectoryBean;

import java.util.*;

/**
 * Created by wuyouxiong on 2017/3/6.
 */
public class WifiEvaluationByDeltaHelper {

    private static final int DEFAULT_AP_NUMBER = 20;

    public static final Double ABNORMAL = 5000.0;

    public static int AP_COUNT = 1;

    public static final String TEST_MAC = "ec26caf3ee9c";

    public static void launch(AtomTrajectoryBean female, AtomTrajectoryBean sample) {


        getApCount(sample);

       /* System.out.println("1. 原始数据");
        printAp(female, TEST_MAC);
        printAp(sample, TEST_MAC);
        System.out.println("2. 筛选出信号最强的若干AP");*/
        //筛选出信号最强的若干AP
        filter(female);
        filter(sample);

       /* printAp(female, TEST_MAC);
        printAp(sample, TEST_MAC);*/

        WifiEvaluationByMagnetic wifiEvaluationByMagnetic = new WifiEvaluationByMagnetic();
        wifiEvaluationByMagnetic.remedyWifiByMagnetic(female, sample);

       /* System.out.println("3. 根据地磁对应关系补齐丢失的Ap之后");
        printAp(female, TEST_MAC);
        printAp(sample, TEST_MAC);
*/
        WifiEvaluationByDelta instance = WifiEvaluationByDelta.getInstance();
        instance.calDistanceForDelta(instance.traj2Delta(female),
                instance.traj2Delta(sample));


      /*  System.out.println("\n4. 计算Ap与其他Ap的差异");
        printAp(female, TEST_MAC);
        printAp(sample, TEST_MAC);*/


        //instance.calDistanceForDelta(originalData.get(0), originalData.get(1));
    }

    /**
     * 只使用信号最强的 {@link WifiEvaluationByDeltaHelper#DEFAULT_AP_NUMBER}个Ap
     *
     * @param traj
     */
    private static void filter(AtomTrajectoryBean traj) {
        List<List<APBean>> aps = traj.getAps();
        List<List<APBean>> res = new ArrayList<>();
        for (List<APBean> ap : aps) {
            Collections.sort(ap, new Comparator<APBean>() {
                @Override
                public int compare(APBean o1, APBean o2) {
                    return ((int) (o2.getRSS()) - (int) (o1.getRSS()));
                }
            });

            if (ap.size() > DEFAULT_AP_NUMBER) {
                ap = ap.subList(0, DEFAULT_AP_NUMBER);
            }
            res.add(ap);
        }

        traj.setAps(res);
    }


    private static void printAp(AtomTrajectoryBean traj, String mac) {
        List<List<APBean>> aps = traj.getAps();
        for (List<APBean> ap : aps) {
            boolean exist = false;
            for (APBean apBean : ap) {
                if (apBean.getMAC().equals(mac)) {
                    exist = true;
                    System.out.printf("%.2f,", apBean.getRSS());
                }
            }
        }

        System.out.println("\n---------------------");
    }


    private static void getApCount(AtomTrajectoryBean traj) {
        Set<String> macs = new HashSet<>();
        List<List<APBean>> aps = traj.getAps();
        for (List<APBean> ap : aps) {
            for (APBean apBean : ap) {
                macs.add(apBean.getMAC());
            }
        }

        AP_COUNT = macs.size();
    }
}
